package com.visiontech.volunteer_center.controller;

import com.visiontech.volunteer_center.pojo.Content_LFRX;
import com.visiontech.volunteer_center.pojo.Content_WMSX;
import com.visiontech.volunteer_center.service.ContentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Source;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/content")
public class ContentController {

    @Autowired
    public ContentService contentService;

    @GetMapping("/index_user")
    public String show_wmsx_user(Model model){
        List<Content_WMSX> contents = contentService.showContent_wmsx();
        model.addAttribute("contents", contents);
        return "/index_user";
    }

    @GetMapping("/index_administrator")
    public String show_wmsx_administrator(Model model)
    {
        List<Content_WMSX> contents = contentService.showContent_wmsx();
        model.addAttribute("contents", contents);
        return "/index_administrator";
    }

    @GetMapping("index_add")
    public String index_add(){
        return "/index_add";
    }

    @GetMapping("/select_user")
    public String select_wmsx_user(@RequestParam("title") String title, Model model){
        List<Content_WMSX> contents = contentService.findByTitle_wmsx(title);
        model.addAttribute("contents", contents);
        return "/index_user";
    }

    @GetMapping("/select_administrator")
    public String select_wmsx_administrator(@RequestParam("title") String title, Model model){
        List<Content_WMSX> contents = contentService.findByTitle_wmsx(title);
        model.addAttribute("contents", contents);
        return "/index_administrator";
    }

    @GetMapping("/delete_wmsx")
    public String deleteById_wmsx(@RequestParam("wmsx_id") Integer wmsx_id){
        if(contentService.deleteById_wmsx(wmsx_id) == 1){
            return "redirect:/content/index_administrator";
        }else return null;
    }

    @PostMapping("/update_wmsx")
    public String updateById_wmsx(@RequestParam("wmsx_id_new") Integer wmsx_id, @RequestParam("title_new") String title,
                                  @RequestParam("link_new") String link, @RequestParam("photo_new") String photo,
                                  @RequestParam(value = "photoFile", required = false)MultipartFile photoFile) throws IOException {
        if(photoFile != null){
            String path = "D:/develop/llrgroup/software-practice/项目代码统一管理/Images/";
            FileCopyUtils.copy(photoFile.getInputStream(), Files.newOutputStream(Paths.get(path + photo)));
        }
        if(contentService.updateById_wmsx(wmsx_id, title, link, photo) == 1){
            return "redirect:/content/index_administrator";
        }
        return null;
    }

    @PostMapping("/insert_wmsx")
    public String insert_wmsx(@RequestParam("title") String title, @RequestParam("link") String link, @RequestParam("photo")String photo, @RequestParam("photoFile") MultipartFile photoFile) throws IOException {
        String path = "D:/develop/llrgroup/software-practice/项目代码统一管理/Images/";
        FileCopyUtils.copy(photoFile.getInputStream(), Files.newOutputStream(Paths.get(path + photo)));
        if(contentService.insertWMSX(title, link, photo) == 1){
            return "redirect:/content/index_administrator";
        }
        return null;
    }

//  ===================================================================================================================

    @GetMapping("/LFRX_user")
    public String show_LFRX_user(){
        return "LFRX_user";
    }

    @GetMapping("/LFRX_administrator")
    public String show_LFRX_administrator(Model model){
        List<Content_LFRX> content_lfrx = contentService.showLFRXContent();
        model.addAttribute("content_lfrx", content_lfrx);
        return "/LFRX_administrator";
    }

    @GetMapping("/selectById_lfrx")
    public String selectById(@RequestParam("lfrx_id") Integer lfrx_id, Model model, HttpServletResponse response) throws IOException {
        Content_LFRX contentLfrx = contentService.findById(lfrx_id);
        if(contentLfrx != null){
            model.addAttribute("content_lfrx", contentLfrx);
            return "/LFRX_administrator";
        }else {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;setchar=utf-8");
            PrintWriter out = response.getWriter();
            out.println("<script> alert('编号不存在！');window.location.href='/content/LFRX_administrator'</script>");
            out.close();
            return null;
        }
    }

    @GetMapping("/selectByName_lfrx")
    public String selectByName(@RequestParam("lfrx_name") String lfrx_name, Model model, HttpServletResponse response) throws IOException {
        List<Content_LFRX> contentLfrx = contentService.findByName(lfrx_name);
        if(!contentLfrx.isEmpty()){
            model.addAttribute("content_lfrx", contentLfrx);
            return "/LFRX_administrator";
        }else {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;setchar=utf-8");
            PrintWriter out = response.getWriter();
            out.println("<script> alert('姓名不存在！');window.location.href='/content/LFRX_administrator'</script>");
            out.close();
            return null;
        }
    }

    @GetMapping("/select_lfrx")
    public String selectLFRX(@RequestParam("lfrx_id") Integer lfrx_id, @RequestParam("lfrx_name") String lfrx_name, Model model, HttpServletResponse response) throws IOException {
        List<Content_LFRX> contentLfrx = contentService.findLFRX(lfrx_id, lfrx_name);
        if(!contentLfrx.isEmpty()){
            model.addAttribute("content_lfrx", contentLfrx);
            return "/LFRX_administrator";
        }else {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;setchar=utf-8");
            PrintWriter out = response.getWriter();
            out.println("<script> alert('信息不存在！');window.location.href='/content/LFRX_administrator'</script>");
            out.close();
            return null;
        }
    }


    @PostMapping("/insert_lfrx")
    public void insert(@RequestParam("name")String name, @RequestParam("sex")String sex, @RequestParam("phone") String phone,
                       @RequestParam("email")String email, @RequestParam("address")String address ,@RequestParam("text")String text, HttpServletResponse response) throws IOException {
        if (contentService.insertLFRX(name, sex, phone, email, address, text) == 1) {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;setchar=utf-8");
            PrintWriter out = response.getWriter();
            out.println("<script> alert('提交成功！');window.location.href='/content/LFRX_user';</script>");
            out.close();
        } else {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;setchar=utf-8");
            PrintWriter out = response.getWriter();
            out.println("<script> alert('信息已存在！');window.location.href='/content/LFRX_user';</script>");
            out.close();
        }
    }

    @GetMapping("/delete_lfrx")
    public String deleteById(@RequestParam("lfrx_id") Integer lfrx_id){
        if(contentService.deleteById_lfrx(lfrx_id) == 1){
            return "redirect:/content/LFRX_administrator";
        }else return null;
    }
}
